package com.service.other;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileConser {
	
	public static void save(String deviceNo,String data,String flag) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String SavePath = ServerConfig.instance().gps_savepath + File.separator
					+ format.format(new Date()) + File.separator + deviceNo
					+ ".txt";
			append(SavePath,data,flag);
	}
	
	private static void append(String path, String content,String flag) {   
        RandomAccessFile randomFile = null;  
        try {
        	File file = new File(path);
        	File folder = new File(file.getParent());
        	if(!folder.exists()){
        		folder.mkdirs();
        	}
            if(!file.exists()){  
            	file.createNewFile();
            }
            randomFile = new RandomAccessFile(path, "rw");        
            long fileLength = randomFile.length();         
            randomFile.seek(fileLength);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            randomFile.write((flag+" at time :"+format.format(new Date())+"\r\n").getBytes());
            randomFile.write((content+"\r\n").getBytes());
        } catch (IOException e) {     
        	Logger.err(e);
        } finally{  
            if(randomFile != null){  
                try {  
                    randomFile.close();  
                } catch (IOException e) {  
                	Logger.err(e);
                }  
            }  
        }  
    }    
}
